import json
from flask import Flask, render_template, request,Response

from apps.controller.UserController import user_bp
from apps.pojo.Entity import db
from apps.service.LoginService import LoginService

# from apps.controller.UserController import user_bp

# app.py


app = Flask(__name__)

# 注册蓝图
app.register_blueprint(user_bp)

# 设置数据库连接
# db = {
#     'user': 'root',
#     'pass': '0620',
#     'host': 'localhost',
#     'port': '3306',
#     'db': 'flask'
# }
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:0620@localhost:3306/flask'

with app.app_context():
    db.init_app(app)
    # db.create_all()


@app.route('/hello')
def hello_world():  # put application's code here
    return 'Hello World!'


@app.route('/')
def login():  # put application's code here
    return render_template('login.html')


@app.route('/dologin', methods=['POST'])
def dologin():  # put application's code here
    param = request.form
    username = param.get('username')
    password = param.get('password')
    result = LoginService.login(username, password)
    # result = LoginService.LoginService.login(username, password)
    # return render_template('index.html', result=result)
    # 数据传不到result里面
    if result:
        return render_template('index.html')
    else:
        return render_template('login.html')


@app.route('/index_v1')
def index_v1():
    return render_template('index_v1.html')


@app.route('/index_v3')
def index_v3():
    return render_template('index_v3.html')


# @app.route('/main/<num>', methods=['GET', 'POST'])
# def main(num):  # put application's code here
#     return render_template('index.html', num=num)
#
#
# @app.route('/index')
# def index():
#     # 使用Response类的构造方法生成响应对象，设置状态码为201，响应类型为text/html
#     # resp = Response(response='Python&Flask',status=201,
#     #                              content_type='text/html;charset=utf-8')
#
#     # 通过make_response()函数生成JSON类型的响应数据
#     user1 = {'name': 'John', 'age': 20}
#     user2 = {'name': 'Mike', 'age': 19}
#     list_user = [user1, user2]
#     # resp = make_response(jsonify({'Python': 'Flask'}), 202)
#     resp = make_response(jsonify(list_user), 202)
#     return resp
#
#
# @app.route('/hello/flask/<name>')
# def greet(name):
#     return f"{url_for('greet', name=name, age=20)}"  # 反向解析视图函数greet()对应的URL
#
#
# @app.route('/hello/python')
# def great():
#     return redirect(url_for("hello_world"))


if __name__ == '__main__':
    app.run(debug=True)
